﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModMini
{
    public partial class HostelEmpAdd : System.Web.UI.Page
    {
        string id = "";
        string account = "";
        string hostelCode = "";       
        MiniBiz miniBiz = new MiniBiz();
        DefDicBiz defDicBiz=new DefDicBiz();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["HostelCode"] != null)
                hostelCode = Request.Params["HostelCode"].ToString();
            if (Request.Params["ID"] != null)
            {
                id = Request.Params["ID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btnNoUse.Enabled = false;
                btnTran.Enabled = false;
            }
            if (!IsPostBack)
            {
                if (hostelCode != "")
                {
                    ddlHostel.Items.Add(new ListItem(hostelCode, hostelCode));                    
                }
                else
                    defDicBiz.InitDropDownList(miniBiz.GetHostelLists(), ddlHostel);
                txtInTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            
                if (id != "")
                {                  
                    BindData(id);
                }
            }
        }
        private void BindData(string id)
        {
            DataTable dt = miniBiz.GetOneHostelEmp(id);
            if (dt.Rows.Count > 0)
            {
                string oldHostelCode = "";
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtFei.Text = dt.Rows[0]["Fei"].ToString();
                oldHostelCode=dt.Rows[0]["HostelCode"].ToString();
                ViewState["VsOldHostelCode"] = oldHostelCode;
                defDicBiz.InitDropDownList(ddlHostel, oldHostelCode);

                txtBedCode.Text = dt.Rows[0]["BedCode"].ToString();
                txtInNum.Text = dt.Rows[0]["InNum"].ToString();
            
                string inTime = dt.Rows[0]["InTime"].ToString();
                if (Utils.RegexRule.IsDate(inTime))
                    txtInTime.Text = Convert.ToDateTime(inTime).ToString("yyyy-MM-dd");
                else txtInTime.Text =inTime;

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();               
            }

        }
        /// <summary>
        /// 自动提取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = new EmployeeBiz().GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        //2011-11-08 可以录入离职人员信息
                       // if (dt.Rows[0]["StateName"].ToString() != "离职")
                       // {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                      //  }
                      //  else
                         //   JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = new EmployeeBiz().GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        //if (dt.Rows[0]["StateName"].ToString() != "离职")
                        //{
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        //}
                       // else
                        //    JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                }
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }

            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            if (miniBiz.IsExistHostelEmp(empCode, "Y"))
            {
                JSUtility.Alert(this, "对不起，改该员工已经有宿舍了，你可以通过转移来调整");
                return;
            }
            string inNum = txtInNum.Text.Trim();           
            if (!RegexRule.IsInt(inNum))
            {
                JSUtility.Alert(this, "入住人数请输入数字");
                return;
            }
            string inTime = txtInTime.Text.Trim();
            if (!RegexRule.IsDate(inTime))
            {
                JSUtility.Alert(this, "请输入正确的日期");
                return;
            }
            string fei = txtFei.Text.Trim();
            if (!RegexRule.IsDecimal(fei))
            {
                JSUtility.Alert(this, "费用请输入数字");
                return;
            }
            string hostCode = ddlHostel.SelectedValue;
            if (hostCode == "0" || string.IsNullOrWhiteSpace(hostCode))
            {
                JSUtility.Alert(this, "宿舍必须要选择！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());           
            sdList.Add("DeptName",txtDeptName.Text.Trim());
            sdList.Add("HostelCode", ddlHostel.SelectedValue);
            sdList.Add("InNum", inNum);
            sdList.Add("Fei", fei);
            sdList.Add("BedCode", txtBedCode.Text.Trim());
            sdList.Add("InTime", inTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (miniBiz.Insert("Mini_HostelEmp", sdList))
                JSUtility.Alert(this, "增加成功");
            else
                JSUtility.Alert(this, "增加失败");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                JSUtility.Alert(this, "无法更新");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string inNum = txtInNum.Text.Trim();
            if (!RegexRule.IsInt(inNum))
            {
                JSUtility.Alert(this, "已经入住人数请输入数字");
                return;
            }
            string inTime = txtInTime.Text.Trim();
            if (!RegexRule.IsDate(inTime))
            {
                JSUtility.Alert(this, "请输入正确的日期");
                return;
            }
            string fei = txtFei.Text.Trim();
            if (!RegexRule.IsDecimal(fei))
            {
                JSUtility.Alert(this, "费用请输入数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ID", id);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("HostelCode", ddlHostel.SelectedValue);
            sdList.Add("InNum", inNum);
            sdList.Add("Fei", fei);
            sdList.Add("BedCode", txtBedCode.Text.Trim());
            sdList.Add("InTime", inTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (miniBiz.Update("Mini_HostelEmp", sdList))
                JSUtility.Alert(this, "增加成功");
            else
                JSUtility.Alert(this, "增加失败");
        }

        protected void btnTran_Click(object sender, EventArgs e)
        {
             if (string.IsNullOrWhiteSpace(id))
            {
                JSUtility.Alert(this, "无法更新");
                return;
            }
             string oldHostelCode = ViewState["VsOldHostelCode"].ToString();
            string hostelCode=ddlHostel.SelectedValue;
            if (oldHostelCode == hostelCode)
            {
                JSUtility.Alert(this, "请选择不同的地址，原来地址为：" + oldHostelCode);
                return;
            }


            SortedDictionary<string, string> sdList2 = new SortedDictionary<string, string>();
            sdList2.Add("ID", id);
            sdList2.Add("Statu", "N");
            sdList2.Add("StatuTime", System.DateTime.Now.ToString("yyyy-MM-dd"));
            sdList2.Add("Account", account);
            if (miniBiz.Update("Mini_HostelEmp", sdList2))
            {
                string empCode = txtEmpCode.Text.Trim();
                if (string.IsNullOrWhiteSpace(empCode))
                {
                    JSUtility.Alert(this, "员工编号必须填写");
                    return;
                }
                if (!new EmployeeBiz().IsExistEmpCode(empCode))
                {
                    JSUtility.Alert(this, "员工编号不存在");
                    return;
                }
                string inNum = txtInNum.Text.Trim();
                if (!RegexRule.IsInt(inNum))
                {
                    JSUtility.Alert(this, "已经入住人数请输入数字");
                    return;
                }
                string inTime = txtInTime.Text.Trim();
                if (!RegexRule.IsDate(inTime))
                {
                    JSUtility.Alert(this, "请输入正确的日期");
                    return;
                }
                string fei = txtFei.Text.Trim();
                if (!RegexRule.IsDecimal(fei))
                {
                    JSUtility.Alert(this, "费用请输入数字");
                    return;
                }
                SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
                sdList.Add("EmpCode", empCode);
                sdList.Add("EmpName", txtEmpName.Text.Trim());
                sdList.Add("DeptName", txtDeptName.Text.Trim());
                sdList.Add("FromHostCode", oldHostelCode);
                sdList.Add("HostelCode", hostelCode);
                sdList.Add("InNum", inNum);
                sdList.Add("Fei", fei);
                sdList.Add("BedCode", txtBedCode.Text.Trim());
                sdList.Add("InTime", inTime);
                sdList.Add("Memo", txtMemo.Text.Trim());
                sdList.Add("Account", account);
                if (miniBiz.Insert("Mini_HostelEmp", sdList))
                    JSUtility.Alert(this, "转宿舍成功");
                else
                    JSUtility.Alert(this, "转宿舍失败");
            }
            else
                JSUtility.Alert(this, "更新旧信息失败");
        }

        protected void btnNoUse_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                JSUtility.Alert(this, "无法更新");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ID", id);
            sdList.Add("Statu", "N");
            sdList.Add("StatuTime", System.DateTime.Now.ToString("yyyy-MM-dd"));
            sdList.Add("Account", account);
            if (miniBiz.Update("Mini_HostelEmp", sdList))
                JSUtility.Alert(this, "操作成功");
            else
                JSUtility.Alert(this, "操作失败");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("HostelEmpAdd.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                if (miniBiz.DeleteHostelEmp(id))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "没有选择信息");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}